import Vue from 'vue'
import Vuex from 'vuex'
import VueRouter  from 'vue-router'
import App from './App.vue'
import Login from './Login.vue'
import UserInfo from '../UserInfo.vue'
import Register from './Register.vue'
import axios from 'axios';


Vue.prototype.$axios=axios;
// 状态管理
Vue.use(Vuex);
// 路由
Vue.use(VueRouter);
// 配置路由
const router=new VueRouter({
  routes:[
    {
      path:'/',
      component:Login,
      name:'login'
    },
    {
      path:'/login',
      component:Login,
      name:'login'
    },{
      path:'/user_info',
      component:UserInfo
    },{
      path:'/register',
      name:'register',
      component:Register
    }
  ]
})
// 配置状态管理
const store=new Vuex.Store({
  state:{
      domain:'https://www.zoratt.com',
      userInfo:{
        avatar:null,
        nickName:null,
        uid:null,
        portarit:null
      }
  },
  mutations:{
    //更新用户信息
    userInfo(state, newUserInfo) {
      state.userInfo=newUserInfo
    }
  }
})

// 设置cookies，增加到vue实例方便全局调用
Vue.prototype.setCookie=(c_name,value,expiredays)=>{
  var exdate=new Date();
  exdate.setTime(exdate.getTime()+expiredays);
  document.cookie=c_name+"="+escape(value)+((expiredays==null)?"":";expires="+exdate.toGMTString());
}

// 获取cookie
function getCookie(name){
  var arr,reg=new RegExp("(^|)"+ name + "=([^;]*)(;|$)");
   arr=document.cookie.match(reg);
  if(arr){
    return arr[2]
  }
  else
  return null
}
Vue.prototype.getCookie=getCookie
// 删除cookie
Vue.prototype.delCookie=(name)=>{
  var exp=new Date();
  exp.setTime(exp.getTime()-10);
  var cval =getCookie(name);
  if(cval!=null)
  document.cookie=name+'='+cval+';'+'expires='+exp.toGMTString()+';';
  console.log(cval)
}
// 挂载实例
new Vue({
  data:{},
  render:h=>h(App),
  router,
  store,
  watch:{
    '$route':'checkLogin'
  },
  created(){
    this.checkLogin();
  },
  methods:{
    checkLogin(){
       //检查是否存在session
      //  if(!this.getCookie('session')){
      //   this.$router.push('/login');
      // }else{
      //   this.$router.push('/user_info');
      // }
    }
  }
}).$mount('#app')